package com.leave.controller;

import java.util.List;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leave.entity.Leave;
import com.leave.entity.Menu;
import com.leave.entity.Role;
import com.leave.entity.User;
import com.leave.service.ILeaveService;
import com.leave.service.IMenuService;
import com.leave.service.IRoleService;
import com.leave.service.IUserService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("")
public class SkipPageController {
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IMenuService menuService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private ILeaveService leaveService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		if (currentUser.isAuthenticated()) {
			return "redirect:/index";
		}
		List<Role> roleList = roleService.getAllRole();
		JSONObject js = new JSONObject();
		js.put("roleList", roleList);
		model.addAttribute("roleList",js.toString());
		
		return "login.jsp";
	}
	
	@RequestMapping("/index")
	public String index(Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		User u = (User) currentUser.getPrincipal();
		Session session = currentUser.getSession();
		String roleName = (String) session.getAttribute("roleName");
		model.addAttribute("roleName",roleName);
		model.addAttribute("u", u);
		List<Menu> menuList = menuService.getMenuListByRoleName(roleName);
		JSONObject js = new JSONObject();
		js.put("menuList", menuList);
		model.addAttribute("menuList", js.toString());
		return "/WEB-INF/jsp/index.jsp";
	}
	
	@RequestMapping("/askForLeaveManagement")
	public String askForLeaveManagement(Model model) {
		return "/WEB-INF/jsp/askForLeaveManagement.jsp";
	}
	
	@RequestMapping("/userManagement")
	public String userManagement(Model model) {
		return "/WEB-INF/jsp/userManagement.jsp";
	}
	
	@RequestMapping("/roleManagement")
	public String roleManagement(Model model) {
		return "/WEB-INF/jsp/roleManagement.jsp";
	}
	
	@RequestMapping("/user_roleManagerment")
	public String user_roleManagerment(Model model) {
		
		List<Role> roleList = roleService.getAllRole();
		model.addAttribute("roleList",roleList);
		
		return "/WEB-INF/jsp/user_roleManagement.jsp";
	}
	
	@RequestMapping("/menuManagerment")
	public String menuManagerment(Model model) {
		return "/WEB-INF/jsp/menuManagerment.jsp";
	}
	
	
	
	@RequestMapping("/processDeployManagement")
	public String processDeployManagement(Model model) {
		return "/WEB-INF/jsp/processDeployManagement.jsp";
	}
	
	
	@RequestMapping("/processDefinitionManagement")
	public String processDefinitionManagement(Model model) {
		return "/WEB-INF/jsp/processDefinitionManagement.jsp";
	}
	
	@RequestMapping("/daibanManagement")
	public String daibanManagement() {
		return "/WEB-INF/jsp/daibanManagement.jsp";
	}
	
	
	@RequestMapping("/banliTask")
	public String banliTask(String taskId,Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String roleName = (String) session.getAttribute("roleName");
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		Leave leave = leaveService.getLeaveByProcessInstanceId(task.getProcessInstanceId());
		User u = userService.getUserNameByLeave(leave);
		model.addAttribute("taskId",taskId);
		model.addAttribute("leave", leave);
		model.addAttribute("user", u);
		model.addAttribute("roleName", roleName);
		return "/WEB-INF/jsp/banliTask.jsp";
	}
	
	@RequestMapping("/yibanManangement")
	public String yibanManangement(Model model) {
		return "/WEB-INF/jsp/yibanManangement.jsp";
	}
	
	
	
	
	
}
